﻿// -----------------------------------------------------------------------
// <copyright file="LocationInStock_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class LocationInStock_DAL:BaseDAL
    {
        
        // Lấy tất cả LocationInStock
        public static List<LocationInStock> GetAll()
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.LocationInStocks);
            return DB.LocationInStocks.ToList();
        }
        // Lấy LocationInStock theo Id
        public static LocationInStock GetDataById(int partID)
        {
            return DB.LocationInStocks.Where(m => m.Parts_ID == partID).FirstOrDefault();
        }
        public static List<LocationInStock> GetDataByPartId(int partID, bool isChange)
        {
            if (isChange)
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.LocationInStocks);

            return DB.LocationInStocks.Where(m => m.Parts_ID == partID).ToList();
        }

        //Thêm một LocationInStock
        public static bool Insert(LocationInStock LocationInStock)
        {
            try
            {
                DB.LocationInStocks.InsertOnSubmit(LocationInStock);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một LocationInStock
        public static bool Delete(LocationInStock LocationInStock)
        {
            try
            {
                DB.LocationInStocks.DeleteOnSubmit(LocationInStock);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static bool DeleteByID(LocationInStock jp)
        {
            LocationInStock objLocationInStock = GetDataById(jp.Parts_ID);
            return Delete(objLocationInStock);
        }
        // Cập nhật LocationInStock
        public static bool Update(LocationInStock LocationInStock)
        {
            try
            {
              //  LocationInStock objLocationInStock = GetDataById(LocationInStock.Parts_ID);
             //   objLocationInStock.Stock_ID = LocationInStock.Stock_ID;
              //  objLocationInStock.Parts_ID = LocationInStock.Parts_ID;
              //  objLocationInStock.Quantity = LocationInStock.Quantity;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static bool isExits(LocationInStock jp)
        {
            LocationInStock objPartJob = null;
            objPartJob = DB.LocationInStocks.Where(j => ((j.Stock_ID == jp.Stock_ID) &&
                (j.Parts_ID == jp.Parts_ID))).FirstOrDefault();
            return objPartJob != null ? true : false;
        }
        public static LocationInStock isExits2(int partID, int stockID)
        {
            LocationInStock objPartJob = null;
            objPartJob = DB.LocationInStocks.Where(j => ((j.Stock_ID == stockID) &&
                (j.Parts_ID == partID))).FirstOrDefault();
            return objPartJob;
        }
        public static List<LocationInStock> GetDataByStockID(int id, bool ischanged)
        {
            if (ischanged)
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.LocationInStocks);
            }
            return DB.LocationInStocks.Where(j => j.Stock_ID == id).ToList();
        }
        public static int TestQuantityPart(int partID, int stockID)
        {
            return DB.LocationInStocks.Where(n => n.Parts_ID == partID && n.Stock_ID == stockID).FirstOrDefault().Quantity.Value;
        }
        public static bool ExitsInStock(int id)
        {
            return DB.LocationInStocks.Where(n => n.Parts_ID == id).FirstOrDefault() != null ? true : false;
        }
    }
}
